import { axios } from "@/plugins/axios";
import merge from "lodash/merge";
import { FileStore } from "./file";
import { User } from "./user";

class ArticleCategory {
  constructor() {
    this.id = 0;
    this.children = []; // good for treeview
    this.category_name = "";
    this.name_key = "category_name" // for ITreeViewItem
    this.parent_id = "";
    this.created_by = new User();
    this.updated_by = new User();
    this.update_time = new Date();
    this.create_time = new Date();
  }

  static fetch(query_args = {}, page = 1, per_page = 10) {
    const url = process.env.VUE_APP_API_URL + "api/v1/article/category";
    let params = { ...query_args, per_page: per_page, page: page };

    return axios.get(url, { params: params }).then((res) => {
      let results = [];
      res.data.results.forEach((item) => {
        const data = merge(new ArticleCategory(), item);
        results.push(data);
      });
      return { total: res.data.total, pages: res.data.pages, results: results };
    });
  }

  static async getById(category_id) {
    const res = await ArticleCategory.fetch({ id: category_id}, 1, 0)
    if(res.total>0){
      return res.results[0]
    }else{
      return null
    }
  }

  createOrUpdate() {
    const url = process.env.VUE_APP_API_URL + "api/v1/article/category";
    const data = Object.assign({}, this);

    if (!data.id || data.id == 0) {
      delete data.id;
    }
    return axios.post(url, data).then((res) => {
      const data = merge(new ArticleCategory(), res.data.data);
      return data;
    });
  }
  delete() {
    const url = process.env.VUE_APP_API_URL + "api/v1/article/category";
    const params = { id: this.id };
    return axios.delete(url, { params: params }).then((res) => {
      return true;
    });
  }
}

class Article {
  constructor() {
    this.id = 0
    this.category_id = 0
    this.category = new ArticleCategory()
    this.subject = "" // 标题
    this.introduction = "" // 简介
    this.content = "" // 内容
    this.cover = new FileStore()
    this.created_by = ""
    this.updated_by = ""
    this.update_time = new Date()
    this.create_time = new Date()
    this.wechat_link = null
  }

  static fetch(query_args = {}, page = 1, per_page = 10) {
    const url = process.env.VUE_APP_API_URL + "api/v1/articles"
    let params = { ...query_args, per_page: per_page, page: page }

    return axios.get(url, { params: params }).then((res) => {
      let results = []
      res.data.results.forEach((item) => {
        const data = merge(new Article(), item)
        results.push(data)
      })
      return { total: res.data.total, pages: res.data.pages, results: results }
    })
  }

  static async getById(article_id) {
    const url = process.env.VUE_APP_API_URL + `api/v1/article/${article_id}`
    const res = await axios.get(url)
    const item = merge(new Article(), res.data)
    return item
  }

  async createOrUpdate() {
    const data = Object.assign({}, this)
    if (!data.id || data.id == 0) {
      delete data.id
      delete data.unique_id
      const url = process.env.VUE_APP_API_URL + "api/v1/articles"
      const res = await axios.post(url, data).then((res) => {
        const data = merge(new Article(), res.data.data)
        return data
      })
      return res
    }else{
      // 修改
      const url = process.env.VUE_APP_API_URL + `api/v1/article/${data.id}`
      const res = await axios.put(url, data).then((res) => {
        const data = merge(new Article(), res.data.data)
        return data
      })
      return res
    }
  }
  async delete() {
    const url = process.env.VUE_APP_API_URL + `api/v1/article/${this.id}`
    await axios.delete(url)
    return true
  }
}

export default Article;
export { ArticleCategory, Article };
